home *** CD-ROM | disk | FTP | other *** search
- /*
- ** Apple Macintosh Developer Technical Support
- **
- ** File: Menu.c
- ** Written by: Eric Soldan
- **
- ** Copyright © 1990-1993 Apple Computer, Inc.
- ** All rights reserved.
- */
-
- /* You may incorporate this sample code into your applications without
- ** restriction, though the sample code has been provided "AS IS" and the
- ** responsibility for its operation is 100% yours. However, what you are
- ** not permitted to do is to redistribute the source as "DSC Sample Code"
- ** after having made changes. If you're going to re-distribute the source,
- ** we require that you make it clear in the source that the code was
- ** descended from Apple Sample Code, but that you've made changes. */
-
-
-
- /*****************************************************************************/
-
-
-
- #include "App.h" /* Get the application includes/typedefs, etc. */
- #include "App.defs.h" /* Get various application definitions. */
- #include "App.protos.h" /* Get the prototypes for application. */
-
- #ifndef __DESK__
- #include <Desk.h>
- #endif
-
- #ifndef __ERRORS__
- #include <Errors.h>
- #endif
-
- #ifndef __MEMORY__
- #include <Memory.h>
- #endif
-
- #ifndef __MENUS__
- #include <Menus.h>
- #endif
-
- #ifndef __TOOLUTILS__
- #include <ToolUtils.h>
- #endif
-
- #ifndef __UTILITIES__
- #include "Utilities.h"
- #endif
-
-
-
- /*****************************************************************************/
-
-
- extern Boolean gQuitApplication;
- extern Boolean gHasAppleEvents;
- extern OSType gAppWindowType;
-
- extern Boolean gLowOnMem;
-
-
-
- /*****************************************************************************/
- /*****************************************************************************/
-
-
-
- /* •• Called by DTS.Lib..framework. •• */
-
- /* Adjust the menu items. We allow the DTS.Lib framework to do most of the work
- ** for us. It will walk the menubar, and for each menu in the menubar, it will
- ** disable all of the menu items and then call the application at either
- ** AdjustMenuItems() (for document and palette windows, or for the no-window case),
- ** or DialogAdjustMenuItems() (for modal dialogs). The application's job is to then
- ** turn on menu items that should be enabled to match the current application state.
- ** The initial Wannabe code for AdjustMenuItems() calls DoAdjustFileMenu() for the
- ** file menu, and DoAdjustEditMenu() for the edit menu. Any other menus that may
- ** be added to Wannabe have all menu items enabled. This allows menus to be added
- ** to the running version of Wannabe and allows them to actually do something.
- ** If the top-most window is a dialog, then all menus are disabled except for the
- ** edit menu. Various items in the edit menu are enabled, depending on if there
- ** is an active TextEdit control, and what is in the clipboard. */
-
- #pragma segment Menu
- void DoAdjustMenus(void)
- {
- if (DoAdjustMBARMenus(FrontWindow(), rMenuBar))
- DrawMenuBar();
- }
-
-
-
- /*****************************************************************************/
-
-
-
- /* This is called when an item is chosen from the menu bar (after calling
- ** MenuSelect or MenuKey). It performs the right operation for each command.
- ** It is good to have both the result of MenuSelect and MenuKey go to one
- ** routine like this to keep everything organized. */
-
- #pragma segment Menu
- Boolean DoMenuCommand(short menuID, short menuItem)
- {
- short undoDepth, numUndos, saveMode, daRefNum, i;
- Str255 daName;
- FileRecHndl frHndl, ff;
- WindowPtr window, ww;
- TEHandle te;
- OSErr err;
- Boolean handled;
- MenuHandle menu;
- Str255 str, fname;
-
- handled = true;
-
- if (window = FrontWindow())
- frHndl = (FileRecHndl)GetWRefCon(window);
- /* frHndl is valid only if it is one of our windows. */
-
- switch (menuID) {
-
- case mApple:
- switch (menuItem) {
- case kStdAbout: /* Bring up alert for About. */
- NewDocumentWindow(nil, 'ABOT', false);
- break;
- default: /* All non-About items in this menu are DAs. */
- GetItem(GetMHandle(mApple), menuItem, daName);
- daRefNum = OpenDeskAcc(daName);
- break;
- }
- break;
-
- case mFile:
- switch (menuItem) {
- case kStdNew:
- if (err = NewDocumentWindow(nil, gAppWindowType, true))
- HCenteredAlert(rErrorAlert, nil, (ModalFilterProcPtr)AlertFilter);
- break;
- case kStdOpen:
- err = OpenDocumentWindow(&frHndl, nil, fsRdWrPerm);
- if ((err) && (err != userCanceledErr))
- HCenteredAlert(rErrorAlert, nil, (ModalFilterProcPtr)AlertFilter);
- break;
- case kStdClose:
- if (IsAppWindow(window)) {
- if (window = FrontWindowOfType(kwIsDocument, true))
- DisposeOneWindow(window, kClose);
- }
- else
- DisposeOneWindow(window, kClose); /* Dispose of DA window. */
- break;
- case kStdSave:
- case kStdSaveAs:
- saveMode = (menuItem == kStdSave) ? kSave : kSaveAs;
- if ((*frHndl)->fileState.refNum == kInvalRefNum)
- saveMode = kSaveAs;
- err = SaveDocument(frHndl, window, saveMode);
- if ((err) && (err != userCanceledErr))
- HCenteredAlert(rErrorAlert, nil, (ModalFilterProcPtr)AlertFilter);
- break;
- case kStdPageSetup:
- DoSetCursor(&qd.arrow);
- PresentStyleDialog(frHndl);
- break;
- case kStdPrint:
- DoSetCursor(&qd.arrow);
- err = noErr;
- if (!(*frHndl)->d.doc.fhInfo.printRecValid)
- err = PresentStyleDialog(frHndl);
- if (!err) {
- err = PrintDocument(frHndl, true, true);
- PrintDocument(nil, false, false);
- }
- if ((err) && (err != userCanceledErr))
- HCenteredAlert(rErrorAlert, nil, (ModalFilterProcPtr)AlertFilter);
- break;
- case kStdQuit:
- gQuitApplication = DisposeAllWindows();
- break;
- default:
- handled = false;
- break;
- }
- break;
-
- case mEdit: /* Call SystemEdit for DA editing & MultiFinder. */
- if (menuItem == kStdViewHier) {
- handled = false;
- break;
- }
- if (IsAppWindow(window)) {
- switch (menuItem) {
- case kStdUndo:
- case kStdRedo:
- case kStdCut:
- case kStdCopy:
- case kStdPaste:
- case kStdClear:
- switch ((*frHndl)->fileState.sfType) {
- /* This is written with the assumption that document types
- ** that demand specific code will be added. The below “if”
- ** illustrates how to handle the edit menu for windows that
- ** have an active TextEdit control. The “else” shows a typical
- ** undo/redo scenario for applications that are using the
- ** hierarchical document package. The clipboard features
- ** are of course document-dependent, so a sample hasn't been
- ** implemented here. For a sample, see DTS.Draw. */
- default:
- if (te = CTEFindActive(window)) {
- if ((*te)->viewRect.left < -8192)
- BeginFrame(window);
- else
- BeginContent(window);
- if (menuItem == kStdUndo)
- CTEUndo();
- else
- CTEClipboard(menuItem - kStdCut + 2);
- EndContent(window);
- }
- else {
- if (menuItem <= kStdRedo) {
- if (!UnmapMItem(mEdit, kStdUndo)) {
- GetUndoInfo(frHndl, &undoDepth, &numUndos);
- DoUndoTask((*frHndl)->d.doc.root, 1 - undoDepth, true);
- }
- else DoUndoTask((*frHndl)->d.doc.root, menuItem - kStdUndo, true);
- }
- else {
- /* Handle rest of edit menu here. */
- }
- }
- break;
- }
- break;
- }
- }
- else SystemEdit(menuItem - 1);
- break;
-
- case mFont:
- menu = GetMHandle(mFont);
- GetItem(menu, menuItem, str);
- ww = GetNextWindow(nil, kDocFileType);
- if (ww) {
- ff = (FileRecHndl)GetWRefCon(ww);
- if (pcmp((*ff)->d.doc.fontName, str)) {
- (*ff)->d.doc.newImage = true;
- pcpy((*ff)->d.doc.fontName, str);
- BeginContent(ww);
- DoImageDocument(ff);
- EndContent(ww);
- SetDocSize(ff, kwNoChange, (*ff)->d.doc.docSize);
- }
- }
- GetItem(menu, menuItem, fname);
- for (i = CountMItems(menu); i; --i) {
- GetItem(menu, i, str);
- if (pcmp(fname, str)) CheckItem(menu, i, false);
- else CheckItem(menu, i, true);
- }
- break;
-
- default:
- handled = false;
- break;
-
- }
-
- return(handled);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Menu
- Boolean DoAdjustFileMenu(WindowPtr window)
- {
- MenuHandle menu;
- FileRecHndl frHndl;
- short enableItem;
-
- menu = GetMHandle(mFile);
- EnableItem(menu, UnmapMItem(mFile, kStdQuit)); /* Gotta be able to quit. */
-
- if (IsDAWindow(window)) {
- EnableItem(menu, UnmapMItem(mFile, kStdClose)); /* Let DAs do a close from the menu. */
- return(false);
- }
-
- if (!gLowOnMem) {
- EnableItem(menu, UnmapMItem(mFile, kStdNew));
- EnableItem(menu, UnmapMItem(mFile, kStdOpen));
- }
-
- if (window = FrontWindowOfType(kwIsDocument, true)) {
- EnableItem(menu, UnmapMItem(mFile, kStdClose));
- frHndl = (FileRecHndl)GetWRefCon(window);
- if ((*frHndl)->fileState.sfType == kDocFileType) {
- enableItem = GetWindowDirty(window);
- if ((*frHndl)->fileState.refNum == kInvalRefNum)
- enableItem = true;
- if (enableItem)
- EnableItem(menu, UnmapMItem(mFile, kStdSave));
- EnableItem(menu, UnmapMItem(mFile, kStdSaveAs));
- }
- EnableItem(menu, UnmapMItem(mFile, kStdPageSetup));
- EnableItem(menu, UnmapMItem(mFile, kStdPrint));
- }
-
- return(false);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Menu
- Boolean DoAdjustEditMenu(WindowPtr window)
- {
- MenuHandle menu;
- Boolean menuEnabled;
- FileRecHndl frHndl;
-
- menu = GetMHandle(mEdit);
-
- if (IsDAWindow(window)) {
- EnableItem(menu, UnmapMItem(mEdit, kStdUndo));
- EnableItem(menu, UnmapMItem(mEdit, kStdCut));
- EnableItem(menu, UnmapMItem(mEdit, kStdCopy));
- EnableItem(menu, UnmapMItem(mEdit, kStdPaste));
- EnableItem(menu, UnmapMItem(mEdit, kStdClear));
- return(false);
- }
-
- if (IsAppWindow(window)) {
- frHndl = (FileRecHndl)GetWRefCon(window);
- switch ((*frHndl)->fileState.sfType) {
- #if VH_VERSION
- case kViewHierFileType:
- CTEEditMenu(&menuEnabled, mEdit, UnmapMItem(mEdit, kStdUndo),
- UnmapMItem(mEdit, kStdCut));
- break;
- #endif
- default:
- EnableItem(menu, UnmapMItem(mEdit, kStdViewHier));
- CTEEditMenu(&menuEnabled, mEdit, UnmapMItem(mEdit, kStdUndo),
- UnmapMItem(mEdit, kStdCut));
- break;
- }
- }
-
- return(false);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Menu
- Boolean DoAdjustFontMenu(WindowPtr window);
- Boolean DoAdjustFontMenu(WindowPtr window)
- {
- #pragma unused (window)
-
- MenuHandle menu;
- FileRecHndl frHndl;
- short i;
- Str255 fname, str;
-
- menu = GetMHandle(mFont);
-
- (*menu)->enableFlags |= 0xFFFFFFFEL;
-
- frHndl = GetNextDocument(nil, kDocFileType);
- if (!frHndl) return(false);
-
- pcpy(fname, (*frHndl)->d.doc.fontName);
- for (i = CountMItems(menu); i; --i) {
- GetItem(menu, i, str);
- if (pcmp(fname, str)) CheckItem(menu, i, false);
- else CheckItem(menu, i, true);
- }
-
- return(false);
- }
-
-
-
-